package it.unical.mat.aec.gui;

import it.unical.mat.aec.model.Test;

import java.awt.Dimension;

import javax.swing.table.AbstractTableModel;

public class TestManageTableModel extends AbstractTableModel {

	private Test test;

	private String columnName[] = { "Id Domanda", "Testo Domanda", "Risposta",
			"Risposta Corretta", "Punteggio", "  " };

	public TestManageTableModel(Test test) {
		this.test = test;
	}

	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex) {

		if (columnIndex == 4)
			return true;

		return false;

	}


	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		try{
		test.getDomande().get(rowIndex).setPunteggio(Double.valueOf(aValue.toString()));
		test.getDomande().get(rowIndex).setCorrect(true);
		fireTableCellUpdated(rowIndex, columnIndex);
		fireTableCellUpdated(rowIndex, columnIndex+1);
		 }catch (NumberFormatException e) {
			// TODO: handle exception
		}
		
	}

	@Override
	public int getColumnCount() {
		return columnName.length;
	}

	@Override
	public String getColumnName(int index) {
		return columnName[index];
	}

	@Override
	public int getRowCount() {
		return test.getDomande().size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		if (columnIndex == 0)
			return test.getDomande().get(rowIndex).getId();
		else if (columnIndex == 1)
			return test.getDomande().get(rowIndex).getTesto();
		else if (columnIndex == 2)
			return test.getDomande().get(rowIndex).getRisposta();
		else if (columnIndex == 3) {
			if (test.getTraccia().getDomanda(
					test.getDomande().get(rowIndex).getId()) != null)
				return test.getTraccia()
						.getDomanda(test.getDomande().get(rowIndex).getId())
						.getRisposta();
		} else if (columnIndex == 4)
			return test.getDomande().get(rowIndex).getPunteggio();
		else if (columnIndex == 5)
			if (test.getDomande().get(rowIndex).isCorrect()) {
				IconImage icon = new IconImage("images/icon/corretta.png",
						new Dimension(20, 20));
				return icon;
			} else {
				IconImage icon = new IconImage("images/icon/noncorretta.png",
						new Dimension(20, 20));
				return icon;
			}

		return null;
	}

}
